package com.financing.platform.web.api;

import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.UserFeedBackDTO;
import com.financing.platform.service.IFpUserFeedBackService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;

@Controller("fpUserFeedBackActionApi")
@RequestMapping("/api/fpUserFeedBack")
@Scope("prototype")
public class FpUserFeedBackAction extends BaseAction {

	@Autowired
	private IFpUserFeedBackService fpUserFeedBackServiceImpl;

	@RequestMapping("/fpUserFeedBackList.shtml")
	@ResponseBody
	public ResultMap fpUserFeedBackList(HttpServletRequest request, HttpServletResponse response, UserFeedBackDTO userFeedBackDTO, Pager pager) {
		List<UserFeedBackDTO> list = Collections.emptyList();
		try {
			userFeedBackDTO.setBegin(pager.getStartIndex());
			userFeedBackDTO.setPageSize(pager.getPageSize());
			
			int count = fpUserFeedBackServiceImpl.userFeedBackListCount();
			pager.setTotalCount(count);
			
			if(count > 0){
				list = fpUserFeedBackServiceImpl.userFeedBackList(userFeedBackDTO);
			}
			
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}

	@RequestMapping("/updateFpUserFeedBack.shtml")
	@ResponseBody
	public Map<?, ?> updateFpUserFeedBack(HttpServletRequest request, HttpServletResponse response, UserFeedBackDTO userFeedBackDTO) {
		DTOdateSet.setUpdateInfo(userFeedBackDTO, request.getSession());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Dict.result, Dict.result_success);
		// 设置用户反馈意见状态为02:已读
		userFeedBackDTO.setStatus("02");
		try {
			fpUserFeedBackServiceImpl.updateUserFeedBack(userFeedBackDTO);
		} catch (SQLException e) {
			log.error("update user feed back status error!", e);
			resultMap.put(Dict.result, Dict.result_error);
		}
		return resultMap;
	}
}
